package com.neuedu.it.teach.service.impl;

import com.neuedu.it.teach.dao.HelpDao;
import com.neuedu.it.teach.dao.UserDao;
import com.neuedu.it.teach.entity.Help;
import com.neuedu.it.teach.entity.User;
import com.neuedu.it.teach.entity.StudentInfo;
import com.neuedu.it.teach.service.HelpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.neuedu.it.teach.dao.StudentInfoDao;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HelpServiceImpl implements HelpService {

    @Autowired
    private HelpDao helpDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private StudentInfoDao studentDao;
    @Override
    @Transactional
    public Help insertHelp(Help help) {
        // 获取用户ID
        int studentId = help.getStudentId();
        StudentInfo student = studentDao.selectStudentsByStudentId(studentId);
        if (student == null) {
            // 如果用户不存在
            return null;
        }
        // 随机生成200-500的奖励值
        help.setReward((int) (Math.random() * 300 + 200));
        helpDao.insertHelp(help);
        return help;
    }

    @Override
    @Transactional
    public boolean updateHelp(Integer helpId,String status) {
        Help help = new Help();
        help.setHelpId(helpId);
        help.setStatus(status);
        return helpDao.updateHelp(help);
    }

    @Override
    @Transactional
    public boolean updateHelp(Help help) {
        return helpDao.updateHelp(help);
    }
    @Override
    @Transactional
    public Help deleteHelp(Help help) {
        helpDao.deleteHelp(help.getHelpId());
        return help;
    }

    @Override
    public Help queryHelp(Help help) {
        return helpDao.queryHelp(help);
    }

    @Override
    public Help queryHelpByUserId(Help help) {
        // 获取用户ID
        int userId = help.getUserId();

        User user = userDao.queryUserById(userId);

        // 判断用户是否是老师
        if (user != null && user.getIsTeacher()==true) {
            // 返回一条信息表示该用户为教师，无求助公告
            return null;
        }

        // 如果不是老师，返回用户的求助公告
        return helpDao.queryHelpByUserId(userId);
    }

    @Override
    public Help queryHelpByHelpId(Help help) {
        return helpDao.queryHelpByHelpId(help.getHelpId());
    }

    @Override
    public Help queryHelpByStatus(Help help) {
        return helpDao.queryHelpByStatus(help.getStatus());
    }



    @Override
    public List<Map<String, Object>> getAllPendingHelp() {
        List<Map<String, Object>> pendingHelps = helpDao.getAllPendingHelp();
        pendingHelps.forEach(help -> {
            Object birthdateObj = help.get("birthdate");
            if (birthdateObj != null) {
                try {
                    if (birthdateObj instanceof java.sql.Date) {
                        java.sql.Date birthdate = (java.sql.Date) birthdateObj;
                        LocalDate birthLocalDate = convertToDate(birthdate).toInstant()
                                .atZone(ZoneId.systemDefault())
                                .toLocalDate();
                        int age = Period.between(birthLocalDate, LocalDate.now()).getYears();
                        help.put("age", age);
                    } else {
                        System.err.println("Unexpected type for 'birthdate': " + birthdateObj.getClass().getName());
                    }
                } catch (Exception e) {
                    System.err.println("Error processing birthdate: " + e.getMessage());
                }
            }
        });
        return pendingHelps;
    }

    private java.util.Date convertToDate(java.sql.Date sqlDate) {
        return new java.util.Date(sqlDate.getTime());
    }

}